package com.taptech.doufu.ui.adapter.personalcenter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.activity.PersonalDynamicActivity;
import com.taptech.doufu.ui.view.UserGradeView;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.NetworkImageView;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-18
 */
public class PersonalCardInfoAapater extends BaseListAdapter {

    public static final int TYPE_ATTENTIONS = 0;
    private Activity activity;
    private String currentKey = "";
    private int showType = -1;

    public int getShowType() {
        return showType;
    }

    public void setShowType(int showType) {
        this.showType = showType;
    }

    public PersonalCardInfoAapater(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.personal_center_adapter_user_card, null);
            holder = new ViewHolder();
            holder.userLabel = (TextView) convertView.findViewById(R.id.personal_center_user_card_label);
            holder.layUserGrade = convertView.findViewById(R.id.layUserGrade);
            holder.userHeader = (NetworkImageView) convertView.findViewById(R.id.personal_center_user_card_icon);
            holder.userHeader.setFullScreen(false);
            holder.userHeader.setRound(true);
            holder.addView = convertView.findViewById(R.id.personal_center_user_card_add_view);

            holder.addBtn = (ImageView) convertView.findViewById(R.id.personal_center_user_card_add_btn);

            holder.addText = (TextView) convertView.findViewById(R.id.personal_center_user_card_add_text);
            holder.medalImg = (ImageView) convertView.findViewById(R.id.personal_author_medal);
            convertView.setTag(holder);
        }
        initData(convertView, position);
        return convertView;
    }

    private void initData(final View convertView, final int position) {
        try {
            final PersonalCardInfo info = (PersonalCardInfo) getItem(position);
            if (info == null)
                return;
            final ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.userHeader.setImageResource(R.drawable.default_user_portrait);
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!info.getUid().equals(AccountService.getInstance().getUserUid())) {
                        UserDetailActivity.Companion.startActivity(v.getContext(),info.getUid());
                    }

                }
            });

            holder.addView.setVisibility(View.VISIBLE);
            initBtnType(holder, info);
            if (showType == TYPE_ATTENTIONS) {
                if (info.getFollowing().equals("1")) {
                    holder.addView.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(final View v) {
                            if (!AccountService.getInstance().isLogin()) {
                                AccountService.getInstance().jumpToLogin();
                                return;
                            }
                            PersonalInfoService.getInstance().cancelAttention(info.getUid(), new HttpResponseListener() {

                                @Override
                                public void handleResponse(int handleType, HttpResponseObject response) {
                                    if (response.getStatus() == 0) {
                                        UIUtil.toastMessage(v.getContext(), "不能做朋友了");
                                        info.setFollowing("0");
                                        initData(convertView, position);
                                    } else {
                                        UIUtil.toastMessage(activity, "取消关注失败");
                                    }

                                }
                            }, activity);

                        }
                    });
                } else {
                    holder.addView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!AccountService.getInstance().isLogin()) {
                                AccountService.getInstance().jumpToLogin();
                                return;
                            }
                            PersonalInfoService.getInstance().addAttention(info.getUid(), new HttpResponseListener() {

                                @Override
                                public void handleResponse(int handleType, HttpResponseObject response) {
                                    if (response.getStatus() == 0) {
                                        PersonalInfoService.getInstance().addMyAttentions(info);
                                        UIUtil.toastMessage(activity, "关注成“攻”");
                                        info.setFollowing("1");
                                        initData(convertView, position);
                                    } else {
                                        UIUtil.toastMessage(activity, "添加关注失败");
                                    }

                                }
                            }, activity);

                        }
                    });

                }
            } else {
                if (info.getFollowing().equals("1")) {
                    holder.addView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(final View v) {
                            if (!AccountService.getInstance().isLogin()) {
                                AccountService.getInstance().jumpToLogin();
                                return;
                            }
                            PersonalInfoService.getInstance().cancelAttention(info.getUid(), new HttpResponseListener() {

                                @Override
                                public void handleResponse(int handleType, HttpResponseObject response) {
                                    if (response.getStatus() == 0) {
                                        UIUtil.toastMessage(activity, "不能做朋友了");
                                        PersonalInfoService.getInstance().delMyAttentions(info.getUid());
                                        info.setFollowing("0");
                                        initData(convertView, position);
                                    } else {
                                        UIUtil.toastMessage(activity, "取消关注失败");
                                    }

                                }
                            }, activity);

                        }
                    });
                } else {
                    holder.addView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!AccountService.getInstance().isLogin()) {
                                AccountService.getInstance().jumpToLogin();
                                return;
                            }
                            PersonalInfoService.getInstance().addAttention(info.getUid(), new HttpResponseListener() {

                                @Override
                                public void handleResponse(int handleType, HttpResponseObject response) {
                                    if (response.getStatus() == 0) {
                                        PersonalInfoService.getInstance().addMyAttentions(info);
                                        UIUtil.toastMessage(activity, "关注成“攻”");
                                        info.setFollowing("1");
                                        initData(convertView, position);
                                    } else {
                                        UIUtil.toastMessage(activity, "添加关注失败");
                                    }

                                }
                            }, activity);

                        }
                    });
                }

            }
            if (AccountService.getInstance().isLogin() && AccountService.getInstance().getUserUid().equals(info.getUid())) {
                holder.addView.setVisibility(View.GONE);
            }

            if (info.getUser_head_img() == null || info.getUser_head_img().equals("") || info.getUser_head_img().equals("null")
                    || info.getUser_head_img().equals("http://imgold2.doufu.la/")) {
            } else {
                // holder.userHeader.setURL(info.getUser_head_img());
                if (!TextUtils.isEmpty(info.getUser_head_img()))
                    ImageManager.displayImage(holder.userHeader, info.getUser_head_img(), 0);
            }
            holder.layUserGrade.setData(PersonalCardInfo.getUserBean(info),false);
            DiaobaoUtil.setMedalImgView(info.getMedal(), holder.medalImg);

            // holder.userName.setText(info.getNickname());
            holder.userLabel.setText(info.getUser_signature());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected class ViewHolder {
        NetworkImageView userHeader;
        ImageView addBtn;
        TextView  userLabel, addText;
        View addView;
        ImageView medalImg;
        UserGradeView layUserGrade;
    }

    private void initBtnType(ViewHolder holder, PersonalCardInfo info) {
        int imageResId = 0;
        String text = "";
        int type = 0;

        if (info.getFollowed().equals("1") && info.getFollowing().equals("1")) {
            type = 2;
        }
        // 我的粉丝
        else if (info.getFollowed().equals("1")) {
            type = 0;
        }
        // 我关注的
        else if (info.getFollowing().equals("1")) {
            type = 1;
        }
        if (type == 0) {
            text = "关注";
            imageResId = R.drawable.personal_attention_flag;
        }
        if (type == 1) {
            text = "已关注";
            imageResId = R.drawable.personal_attentioned_flag;
        }
        if (type == 2) {
            text = "萌友";
            imageResId = R.drawable.personal_attention_fans_flag;
        }

        holder.addBtn.setImageResource(imageResId);
        holder.addText.setText(text);

    }

    public void setSearchContent(String currentKey) {
        this.currentKey = currentKey;
    }
}
